package logic;

import gui.Screen;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.List;

import data.User;

/**
 * Class ATMLogin and its instance variables
 * 
 * @author Niels - niha12ai
 * 
 */
public class ATMLogin {
	private Screen screen;
	private NemIDGenerator nemIDGenerator;
	private InterestAccruel interestAccruel;
	private boolean userAuthenticated;
	private List<User> results;

	/**
	 * Constructor containing screen param and ActionListeners
	 * 
	 * @wbp.parser.entryPoint
	 */
	public ATMLogin(Screen screen) {
		this.screen = screen;
		screen.getLogin().addActionListener(new LoginActionListener());
		screen.getNemid().addActionListener(new NemidActionListener());

	}

	/**
	 * 
	 * Runs Class ATMLogin This is where the Interest is added by date
	 */
	public void run() {
		screen.show(Screen.LOGIN);
		screen.setVisible(true);

		Calendar cal = Calendar.getInstance();
		cal.getTime();

		int day = cal.get(Calendar.DAY_OF_MONTH);

		if (day == 1) {

			InterestAccruel ia = new InterestAccruel();
			ia.interestAccruel();

		}
	}

	/**
	 * Class containing Login JButtons, their ActionListeners, their
	 * functionalities and exception handling
	 * 
	 * @author Niels
	 * 
	 */
	private class LoginActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			if (e.getSource() == screen.getLogin().getBtnLogin()) {
				List<User> users = screen.getModelClass().getDbConnection()
						.getUsers();
				String typedUserName = screen.getLogin().getTxtBrugerID()
						.getText();
				String typedPassword = screen.getLogin().getTxtPassword()
						.getText();

				for (User user : users) {

					if (user.getCpr_number().equalsIgnoreCase(typedUserName)
							&& user.getPassword().equalsIgnoreCase(
									typedPassword)) {

						screen.getModelClass().setCurrentUser(user);

						screen.getNemid().getTxtNemid().setText("");
						screen.show(screen.NEMID);
						screen.getLogin().getLblFailedlogin().setVisible(false);
						nemIDGenerator = new NemIDGenerator();
						screen.getNemid().getLblKey()
								.setText("" + nemIDGenerator.getNemid());

						break;

					} else if (typedUserName.equalsIgnoreCase("Admin")
							&& typedPassword.equalsIgnoreCase("1337")) {
						screen.show(screen.ADMINMENU);
						screen.getLogin().getLblFailedlogin().setVisible(false);

					} else {
						screen.show(screen.LOGIN);
						screen.getLogin().getLblFailedlogin().setVisible(true);
						screen.getLogin().getTxtBrugerID().setText("");
						screen.getLogin().getTxtPassword().setText("");
					}
				}
			}
		}
	}

	/**
	 * Class containing Nemid JButtons, their ActionListeners, their
	 * functionalities and exception handling
	 * 
	 * @author Niels
	 * 
	 */
	private class NemidActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			/**
			 * authenticates user through the database
			 */
			if (e.getSource() == screen.getNemid().getBtnNemidLogin()) {
				try {
					String typedCode = screen.getNemid().getTxtNemid()
							.getText();
					int key = Integer.parseInt(typedCode);

					userAuthenticated = nemIDGenerator.authenticateKey(key);

					if (nemIDGenerator.authenticateKey(Integer.parseInt(screen
							.getNemid().getTxtNemid().getText()))) {

						screen.show(screen.USERMENU);
						screen.getNemid().getLblFailNemID().setVisible(false);

					} else {
						screen.getNemid().getTxtNemid().setText("");
						nemIDGenerator = new NemIDGenerator();
						screen.getNemid().getLblKey()
								.setText("" + nemIDGenerator.getNemid());
						screen.getNemid().getLblFailNemID().setVisible(true);
					}
				} catch (NumberFormatException e1) {

				}
			}
		}
	}
}
